/**
 * 
 */
package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ac.svn.accesscontrol.store.AccessFile;
import ac.svn.accesscontrol.store.Dictionary;
import ac.svn.accesscontrol.utils.Utility;

/**
 * @author Enigmanic
 *
 */
public class ConfigFileSelectorDialog extends JDialog {

	/**
	 * 
	 */

	//private ConfigFileSelectorDialog Iam = this;

	public static final int OK_OPTION 			= 1;
	public static final int CANCEL_OPTION = 2;

	private int selectedOption = CANCEL_OPTION;

	private static final long serialVersionUID = 1L;
	private JTextField fileNameTxt;
	private JTextField filePathTxt;
	private JTextArea notesTxt;

	private JButton browseButton;
	private JButton openButton;

	private JCheckBox asDefaultCheckBox; 
	
	private JList configFileListView;
	private DefaultListModel listModel;

	public ConfigFileSelectorDialog()
	{
		super((Frame)null, true);
		setTitle("Select configuration file");
		initComponents();
	}

	private void initComponents() {
		setLayout(new BorderLayout(5,5));
		setBackground(Color.DARK_GRAY);
		setSize(600,500);
		setLocation();

		Container c = getContentPane();

		JLabel title = new JLabel("<HTML>Select the configuration file you want to analyse, new location can also be added. Just fill name and path of configuration file and press Open</HTML>");
		title.setIcon(Utility.getImageIcon("images/config.png"));
		title.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.RAISED),"Welcome to Smart CM"));
		c.add(title,BorderLayout.NORTH);

		JPanel bodyPanel = new JPanel(new BorderLayout(5, 5),true);
		JPanel panel = new JPanel(new GridLayout(2, 1,2, 2),true);

		fileNameTxt = new JTextField(10);
		filePathTxt 	  = new JTextField(25);
		browseButton = new JButton("Browse..");
		notesTxt = new JTextArea();

		JPanel p = new JPanel(new BorderLayout(2, 2));
		p.add(new JLabel("Name  ",JLabel.CENTER),BorderLayout.WEST);
		p.add(fileNameTxt,BorderLayout.CENTER);
		panel.add(p);

		p = new JPanel(new BorderLayout(2, 2));
		p.add(new JLabel("Path    ",JLabel.CENTER),BorderLayout.WEST);
		p.add(filePathTxt,BorderLayout.CENTER);
		panel.add(p);
		panel.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED),"Current Config file"));

		bodyPanel.add(panel,BorderLayout.NORTH);

		JScrollPane scrollPane = new JScrollPane(notesTxt);
		scrollPane.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED),"Notes"));
		bodyPanel.add(scrollPane,BorderLayout.CENTER);

		listModel = new DefaultListModel();
		configFileListView = new JList(listModel);
		configFileListView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane = new JScrollPane(configFileListView);
		scrollPane.setBorder(new TitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED),"Recent Config files"));
		bodyPanel.add(scrollPane,BorderLayout.SOUTH);

		openButton = new JButton("Open");

		asDefaultCheckBox = new JCheckBox("Set as Default."); 

		JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT),true);
		pnl.add(asDefaultCheckBox);

		panel = new JPanel(new BorderLayout(5,5));
		panel.add(pnl,BorderLayout.CENTER);
		
		pnl = new  JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5),true);
		pnl.add(browseButton);
		pnl.add(openButton);
		panel.add(pnl,BorderLayout.EAST);

		
		c.add(bodyPanel,BorderLayout.CENTER);
		c.add(panel,BorderLayout.SOUTH);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				selectedOption = CANCEL_OPTION;				
			}
		});

		openButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(false== filePathTxt.getText().isEmpty())
				{
					AccessFile configFile = new AccessFile();
					configFile.setAccessFileLocation(filePathTxt.getText().trim());
					configFile.setAccessFileName(fileNameTxt.getText().trim());
					configFile.setNotes(notesTxt.getText());
					Dictionary.getDictionary().addAccessFile(configFile);
					setConfigFileList();
					configFileListView.setSelectedValue(configFile, true);
				}
				selectedOption = OK_OPTION;
				setVisible(false);
				
			}
		});	

		browseButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int selectedOption = fileChooser.showOpenDialog(null);
				if(JFileChooser.CANCEL_OPTION != selectedOption)
				{
					String seletedFile = fileChooser.getSelectedFile().getAbsolutePath();
					filePathTxt.setText(seletedFile);
					fileNameTxt.setText(fileChooser.getSelectedFile().getName()); 
				}
			}
		});

		configFileListView.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent le) {
				if(!le.getValueIsAdjusting())
				{
					AccessFile configFile = (AccessFile) configFileListView.getSelectedValue();
					if(null != configFile)
					{
						fileNameTxt.setText(configFile.getAccessFileName());
						filePathTxt.setText(configFile.getAccessFileLocation());
						notesTxt.setText(configFile.getNotes());
					}
				}
			}
		});

		configFileListView.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyPressed(KeyEvent e) {
				if(KeyEvent.VK_DELETE == e.getKeyCode())
				{
					AccessFile  accessFile = (AccessFile) configFileListView.getSelectedValue();
					if(null != accessFile)
					{
						Dictionary.getDictionary().getAccessFiles().remove(accessFile);
						setConfigFileList();
					}
				}
			}
		});
	}
	public int showConfigSelectionDialog()
	{
		selectedOption = CANCEL_OPTION;
		setConfigFileList();
		setVisible(true);
		return selectedOption;
	}

	public void setConfigFileList()
	{
		Set<AccessFile> configFiles = Dictionary.getDictionary().getAccessFiles();		
		listModel.clear();
		for(AccessFile configFile: configFiles)
		{
			listModel.addElement(configFile);
		}
		/*SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
*/				SwingUtilities.updateComponentTreeUI(configFileListView);				
/*			}
		});		
*/	}

	public AccessFile getSelectedFile()
	{
		return (AccessFile) configFileListView.getSelectedValue();
	}


	private void setLocation() 
	{
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		setLocation((int)(toolkit.getScreenSize().getWidth()/2 - getWidth()/2), (int)(toolkit.getScreenSize().getHeight()/2 - getHeight()/2));
	}

	public boolean isSetDefault()
	{
		return asDefaultCheckBox.isSelected() ;
	}
	
	public static void main(String arg[])
	{
		ConfigFileSelectorDialog dialog = new ConfigFileSelectorDialog();
		dialog.setVisible(true);
	}

	

}
